# Maintainer: Sergey Popov <popov.sa@gmail.com>

_realname=vtk
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=6.3.0
pkgrel=2
pkgdesc="A software system for 3D computer graphics, image processing and visualization (mingw-w64)"
arch=('any')
url="http://www.vtk.org/"
license=("BSD")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-expat"
         "${MINGW_PACKAGE_PREFIX}-ffmpeg"
         "${MINGW_PACKAGE_PREFIX}-fontconfig"
         "${MINGW_PACKAGE_PREFIX}-freetype"
         "${MINGW_PACKAGE_PREFIX}-hdf5"
         "${MINGW_PACKAGE_PREFIX}-intel-tbb"
         "${MINGW_PACKAGE_PREFIX}-jsoncpp"
         "${MINGW_PACKAGE_PREFIX}-libjpeg"
         "${MINGW_PACKAGE_PREFIX}-libpng"
         "${MINGW_PACKAGE_PREFIX}-libogg"
         "${MINGW_PACKAGE_PREFIX}-libtheora"
         "${MINGW_PACKAGE_PREFIX}-libtiff"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-qt5"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-boost"
             "${MINGW_PACKAGE_PREFIX}-libmariadbclient"
             "${MINGW_PACKAGE_PREFIX}-postgresql"
             "${MINGW_PACKAGE_PREFIX}-python2"
             "${MINGW_PACKAGE_PREFIX}-tk")
optdepends=("${MINGW_PACKAGE_PREFIX}-boost: InfovisBoost and InfovisBoostGraphAlgorithms modules"
            "${MINGW_PACKAGE_PREFIX}-libmariadbclient: interface to MariaDB"
            "${MINGW_PACKAGE_PREFIX}-postgresql: interface to PostgreSQL"
            "${MINGW_PACKAGE_PREFIX}-python2: Python bindings"
            "${MINGW_PACKAGE_PREFIX}-python2-matplotlib: Matplotlib renderer"
            "${MINGW_PACKAGE_PREFIX}-tk: TCL bindings, Python Tk widgets")
source=(#${_realname}-${pkgver}.tar.gz::"https://github.com/Kitware/VTK/archive/v${pkgver}.tar.gz"
        http://www.vtk.org/files/release/${pkgver%.*}/VTK-${pkgver}.tar.gz
        "vtk-mingw-w64.patch")
sha256sums=('92a493354c5fa66bea73b5fc014154af5d9f3f6cee8d20a826f4cd5d4b0e8a5e'
            'db3163012fc7d90f6ffd8300a7a306a609e7c69a88a1dcfb9cb5b5df4391e3ad')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"

  patch -Np1 -i "$srcdir/vtk-mingw-w64.patch"
}

build() {
  CFLAGS+=" ${CPPFLAGS}"
  CXXFLAGS+=" ${CPPFLAGS} -std=gnu++11"
  [[ "$CARCH" = 'i686' ]] && {
    _cmakeopts=('-DVTK_USE_64BIT_IDS=OFF')
  }
  [[ "$CARCH" = 'x86_64' ]] && {
    _cmakeopts=('-DVTK_USE_64BIT_IDS=ON')
  }

  [[ -d ${srcdir}/build-${CARCH} ]] && rm -rf ${srcdir}/build-${CARCH}
  mkdir -p ${srcdir}/build-${CARCH} && cd ${srcdir}/build-${CARCH}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=;-DVTK_INSTALL_QT_PLUGIN_DIR=;-DVTK_INSTALL_TCL_DIR=" \
  ${MINGW_PREFIX}/bin/cmake.exe -Wno-dev \
    -G"MSYS Makefiles" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DVTK_INSTALL_QT_PLUGIN_DIR=${MINGW_PREFIX}/share/qt5/plugins/designer \
    -DVTK_INSTALL_TCL_DIR=${MINGW_PREFIX}/lib/vtk-${pkgver%.*} \
    -DBUILD_DOCUMENTATION=OFF \
    -DBUILD_EXAMPLES=OFF \
    -DBUILD_TESTING=OFF \
    -DVTK_LEGACY_REMOVE=OFF \
    -DVTK_Group_Imaging=ON \
    -DVTK_Group_MPI=OFF \
    -DVTK_Group_Qt=ON \
    -DVTK_Group_Rendering=ON \
    -DVTK_Group_StandAlone=ON \
    -DVTK_Group_Tk=ON \
    -DVTK_Group_Views=ON \
    -DVTK_Group_Web=OFF \
    -DVTK_USE_SYSTEM_EXPAT=ON \
    -DVTK_USE_SYSTEM_FREETYPE=ON \
    -DVTK_USE_SYSTEM_GL2PS=OFF \
    -DVTK_USE_SYSTEM_HDF5=ON \
    -DVTK_USE_SYSTEM_JPEG=ON \
    -DVTK_USE_SYSTEM_JSONCPP=ON \
    -DVTK_USE_SYSTEM_LIBPROJ4=OFF \
    -DVTK_USE_SYSTEM_LIBXML2=ON \
    -DVTK_USE_SYSTEM_NETCDF=OFF \
    -DVTK_USE_SYSTEM_OGGTHEORA=ON \
    -DVTK_USE_SYSTEM_PNG=ON \
    -DVTK_USE_SYSTEM_TIFF=ON \
    -DVTK_USE_SYSTEM_ZLIB=ON \
    -DVTK_USE_VIDEO_FOR_WINDOWS=ON \
    -DModule_vtkIOFFMPEG=ON \
    -DModule_vtkIOMySQL=ON \
    -DMYSQL_INCLUDE_DIRECTORIES=${MINGW_PREFIX}/include/mariadb \
    -DMYSQL_LIBRARY=${MINGW_PREFIX}/lib/libmariadb.dll.a \
    -DModule_vtkInfovisBoost=ON \
    -DModule_vtkInfovisBoostGraphAlgorithms=ON \
    -DModule_vtkIOODBC=OFF \
    -DModule_vtkIOPostgreSQL=ON \
    -DModule_vtkPythonInterpreter=ON \
    -DModule_vtkFiltersReebGraph=ON \
    -DModule_vtkRenderingFreeTypeFontConfig=ON \
    -DModule_vtkRenderingMatplotlib=ON \
    -DVTK_WRAP_JAVA=OFF \
    -DVTK_WRAP_PYTHON=ON \
    -DVTK_WRAP_TCL=ON \
    -DVTK_QT_VERSION=5 \
    -DPYTHON_EXECUTABLE=${MINGW_PREFIX}/bin/python2.7.exe \
    -DPYTHON_LIBRARY=${MINGW_PREFIX}/lib/libpython2.7.dll.a \
    -DPYTHON_INCLUDE_DIR=${MINGW_PREFIX}/include/python2.7 \
    -DVTK_SMP_IMPLEMENTATION_TYPE=TBB \
    -DCMAKE_RC_FLAGS=--use-temp-file \
    -DCMAKE_BUILD_TYPE=Release \
    -DBUILD_SHARED_LIBS=ON \
    ${_cmakeopts[@]} \
    ../${_realname}-${pkgver}

  make
}

package() {
  cd "${srcdir}/build-${CARCH}"
  make -j1 DESTDIR=${pkgdir} install

  #local PREFIX_DEPS=$(cygpath -am ${MINGW_PREFIX})

  pushd ${pkgdir}${MINGW_PREFIX}/lib/cmake/${_realname}-${pkgver%.*} > /dev/null
  sed -s 's|Qt5::|Qt5|g' -i ./VTKTargets*.cmake
  #sed -s "s|${PREFIX_DEPS}|\${_IMPORT_PREFIX}|g" -i ./VTKTargets.cmake
  #find . -name '*.cmake' -exec \
  #  sed -s "s|${PREFIX_DEPS}|\${VTK_INSTALL_PREFIX}|g" -i {} \;
  popd > /dev/null

  install -Dm644 ${srcdir}/${_realname}-${pkgver}/Copyright.txt \
    ${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/Copyright.txt
}
